﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.Planner.CApAPPSegmentPreProcessingCommand
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Diagnostics;


namespace DnProcessSimulateCommands.AutomaticPathPlanner.Planner;

public class CApAPPSegmentPreProcessingCommand
{
  private KiAppPathPlanningParamsEx m_pathPlanningParams;

  public CApAPPSegmentPreProcessingCommand(CApAPPSubTask i_subTask)
  {
    this.IsStarted = false;
    this.IsFinished = false;
    this.WasPreProcessingCancelled = false;
    this.WasPreProcessingSuccessful = false;
    this.WasPreProcessingTimeOut = false;
    this.SubTask = i_subTask;
  }

  public CApAPPSubTask SubTask { get; set; }

  public bool IsFinished { get; set; }

  public bool IsStarted { get; set; }

  public bool WasPreProcessingSuccessful { get; set; }

  public bool WasPreProcessingTimeOut { get; set; }

  public bool WasPreProcessingCancelled { get; set; }

  public void Cancel() => this.WasPreProcessingCancelled = true;

  private Stopwatch ElapsedTimer { get; set; }

  private double Progress { get; set; }

  public CApAPPPlanSuccess PlanSuccess()
  {
    CApAPPPlanSuccess capAppPlanSuccess = CApAPPPlanSuccess.SUCCESS_PREPROCESSING;
    if (this.WasPreProcessingTimeOut)
      capAppPlanSuccess = CApAPPPlanSuccess.TIMEOUT_ERROR;
    else if (this.WasPreProcessingCancelled)
      capAppPlanSuccess = CApAPPPlanSuccess.SKIP;
    else if (!this.WasPreProcessingSuccessful && this.IsFinished)
      capAppPlanSuccess = CApAPPPlanSuccess.WAIT_BEFORE_START;
    else if (!this.IsFinished)
      capAppPlanSuccess = this.IsStarted ? CApAPPPlanSuccess.WAIT : CApAPPPlanSuccess.WAIT_BEFORE_START;
    return capAppPlanSuccess;
  }

  public void onIdle()
  {
    if (this.WasPreProcessingCancelled)
      this.EndPlanning(false);
    else
      this.ContinuePlanning();
  }

  public double GetProgress() => this.IsFinished ? 1.0 : this.Progress;

  public void OnStartThread()
  {
    this.IsStarted = true;
    this.IsFinished = false;
    this.ElapsedTimer = new Stopwatch();
    this.ElapsedTimer.Start();
    this.Progress = 0.0;
    while (!this.IsFinished)
    {
      this.onIdle();
      if (this.ElapsedTimer.ElapsedMilliseconds > 1000L)
      {
        double num = 0.0;
        KiAppSegmentPreProcessingPlanCommandEx.GetProgressPreProcessing(this.SubTask.KiSubTask, ref num);
        this.Progress = num;
        this.ElapsedTimer.Restart();
      }
    }
  }

  public virtual bool StartPlanning()
  {
    bool flag1 = true;
    this.IsStarted = true;
    this.IsFinished = false;
    bool flag2 = this.DoStartPlanning(this.SubTask);
    if (!flag1 & flag2)
    {
      this.WasPreProcessingSuccessful = true;
      this.IsFinished = false;
    }
    if (!flag2)
    {
      this.WasPreProcessingSuccessful = false;
      this.IsFinished = true;
    }
    return flag2;
  }

  protected virtual bool DoStartPlanning(CApAPPSubTask subTask)
  {
    return KiAppSegmentPreProcessingPlanCommandEx.StartPreProcessing(subTask.KiSubTask);
  }

  private void EndPlanning(bool i_success)
  {
    this.DoEndPlanning(this.SubTask, i_success);
    this.IsStarted = false;
    this.IsFinished = true;
    this.WasPreProcessingSuccessful = i_success;
  }

  protected virtual void DoEndPlanning(CApAPPSubTask subTask, bool i_success)
  {
    KiAppSegmentPreProcessingPlanCommandEx.EndPreProcessing(subTask.KiSubTask, i_success);
  }

  private void ContinuePlanning()
  {
    bool o_shouldContinue = false;
    bool i_success = this.DoContinuePlanning(this.SubTask, ref o_shouldContinue);
    if (o_shouldContinue && i_success)
      return;
    this.EndPlanning(i_success);
  }

  protected virtual bool DoContinuePlanning(CApAPPSubTask subTask, ref bool o_shouldContinue)
  {
    return KiAppSegmentPreProcessingPlanCommandEx.ContinuePreProcessing(subTask.KiSubTask, ref o_shouldContinue);
  }

  private bool isTimeoutReached() => false;

  public enum EPlanSuccess
  {
    SUCCESS,
    SKIP,
    CLASH_ERROR,
    TIMEOUT_ERROR,
    WAIT,
    WAIT_BEFORE_START,
  }
}
